from models import QueryOneInstance, Blog, User, QueryOneRelatedInstance, QueryMultipleInstances, Comment, Personal, \
    Role


def PK_OnePost(pid):
    blog = QueryOneInstance(Blog,{'id':pid})

    author = QueryOneRelatedInstance(User,{'id':blog.uid},blog)

    data = {
        "t_pid": blog.id,
        "t_time": blog.time,
        "t_sort": blog.sort,
        "t_title": blog.title,
        "t_content": blog.content,
        "t_author": author.username,

        "b_role": blog.role,
        "b_parts": blog.part,

        "b_checked": blog.checked,
        "b_passed": blog.passed,
    }
    return data

def PK_OnePostWithComment(pid):
    post_ = PK_OnePost(pid)

    comment = QueryMultipleInstances(Comment,{'pid':pid})
    list=[]
    for i in comment:
        author = QueryOneInstance(User,{'id':i.uid})

        list.append({
            "t_cid":i.id,
            "t_time":i.time,
            "t_content": i.content,

            "t_uid":i.uid,
            "t_author": author.username,

            "i_icon": QueryOneRelatedInstance(Personal,{'uid':i.uid},author).img_icon,

            "t_role": QueryOneRelatedInstance(Role,{'uid':i.uid},author).role,
            "t_part":QueryOneRelatedInstance(Role,{'uid':i.uid},author).part,
        })

    post_['a_comment']=list
    post_['s_comment']=len(list)
    return post_